package com.weihuachao.repair;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioGroup;

import com.lidroid.xutils.http.RequestParams;
import com.weihuachao.app.WorkAdapter;
import com.weihuachao.entity.Work;
import com.weihuachao.https.WorkRequestCallBck;
import com.weihuachao.https.XUtils;

import java.util.ArrayList;
import java.util.List;

public class MyWorkActivity extends FragmentActivity {

    private RadioGroup radioGroup;
    private ListView listView;
    private WorkAdapter adapter;
    private Button back_mywork;
    private List<Work> workList = new ArrayList<>();
    private int workState = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        XUtils.init(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mywork);
        back_mywork = (Button) findViewById(R.id.back_mywork);
        radioGroup = (RadioGroup) findViewById(R.id.changestate);
        listView = (ListView) findViewById(R.id.worklistView);
        workState = 0;
        initData();
        back_mywork.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(MyWorkActivity.this, WorkDetailsActivity.class);
                intent.putExtra("worktitle", workList.get(i).getTitle());
                intent.putExtra("worktexr", workList.get(i).getText());
                startActivity(intent);
            }
        });
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                int radioid = radioGroup.getCheckedRadioButtonId();
                int tmp = 0;
                if (radioid == R.id.radioButton1) {
                    tmp = 0;
                } else if (radioid == R.id.radioButton2) {
                    tmp = 1;
                } else {
                    tmp = 2;
                }
                if (workState != tmp) {
                    workState = tmp;
                    initData();
                }
            }
        });
    }

    private void initData() {
        RequestParams params = new RequestParams();
        params.addBodyParameter("work.state", String.valueOf(workState));
        params.addBodyParameter("work.uid", String.valueOf(com.weihuachao.app.application.getUid()));
        XUtils.send(XUtils.GETWORK, params, new WorkRequestCallBck() {
                    @Override
                    public void success(List<Work> data) {
                        workList.clear();
                        for (int i = data.size() - 1; i >= 0; --i) {
                            workList.add(data.get(i));
                        }
                        adapter = new WorkAdapter(MyWorkActivity.this, R.layout.item, workList);
                        listView.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                    }
                }
        );
    }
}
